package org.example;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * ClassName: ResponseController
 * Package: org.example
 * Description:
 *
 *通过 HttpServletResponse 响应对象，设置响应信息
 *
 * @Author SherryAn
 * @Create 2025/8/9 13:38
 */
@RestController
public class ResponseController {
    @RequestMapping(value = "/response")

//    方式一：通过HttpServletResponse对象，设置响应信息
    public String response(HttpServletResponse response){
//        1.设置响应状态码
        response.setStatus(401);
        //2.设置响应头
        response.setHeader("name","SherryAn");
        //3.设置响应体
        response.setContentType("text/html;charset=utf-8");

        return "OK!";
    }
//      方式二：通过ResponseEntity对象，设置响应信息,这是SpringBoot推荐的方式
    @RequestMapping(value = "/response2")
    public ResponseEntity<String> response(){

        return ResponseEntity
                .status(401)
                .header("name", "SherryAn")
                .body("Hello response");
    }
}
